package com.link.base.wechat.wxmessage.service;

import com.link.base.wechat.wxlog.model.WxLogModule;
import com.link.base.wechat.wxlog.service.WxLogService;
import com.link.base.wechat.wxmessage.model.WxMessage;
import com.link.base.wechat.wxmessage.model.WxTemplateMsg;
import com.link.base.wechat.wxmessage.util.WxMsgUtil;
import com.link.base.wechat.wxtoken.model.WxToken;
import com.link.base.wechat.wxtoken.service.WxTokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 微信公众号消息管理实现类
 *
 * @author lujiang
 * @date 2019/03/6 18:45
 */
@Service
public class WxMessageServiceImpl implements WxMessageService {

    @Resource
    private WxLogService wxLogService;

    @Resource
    private WxTokenService wxTokenService;

    @Override
    public int wxSendMsg(WxMessage wxMessage, String wxAppId) throws Exception {
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        int num = WxMsgUtil.wxSendMsg(wxMessage, wxToken.getAccess_token());
        wxLogService.log(WxLogModule.wxLogServiceMsg, "客服消息",
            "发送客服消息，内容 => " + wxMessage.getContents(), wxAppId, wxToken.getCorpid());
        return num;
    }

    @Override
    public int sendTempMsg(String wxAppId, WxTemplateMsg templateMsg) throws Exception {
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        int num =  WxMsgUtil.sendTempMsg(wxToken.getAccess_token(), templateMsg);
        wxLogService.log(WxLogModule.wxLogTemplateMsg, "模板消息",
            "发送模板消息，模板Id => " + templateMsg.getTemplateId(), wxAppId, wxToken.getCorpid());
        return num;
    }

    @Override
    public int sendMsg(WxMessage wxMessage, String wxAppId) throws Exception {
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        int num = WxMsgUtil.sendMsg(wxToken.getAccess_token(),wxMessage);
        wxLogService.log(WxLogModule.wxLogMassMsg, "群发消息",
                "群发消息，消息内容 => " + wxMessage.getContents(), wxAppId, wxToken.getCorpid());
        return num;
    }

    @Override
    public int sendAllForTag(String wxAppId, String jsonString) throws Exception {
        WxToken wxToken = wxTokenService.getWxAccessTokenByAppId(wxAppId);
        int num = WxMsgUtil.sendAllForTag(wxToken.getAccess_token(),jsonString);
        wxLogService.log(WxLogModule.wxLogMassMsg, "群发消息",
                "群发消息，消息内容 => " + jsonString, wxAppId, wxToken.getCorpid());
        return num;
    }
}
